<script setup>
import axios from 'axios';
import { ref, onMounted, computed } from 'vue';
import { useRouter, useRoute } from 'vue-router'
import { h } from 'vue'
import { ElMessage } from 'element-plus'
import TimeBtn from '../button/timeBtn.vue'
import {useTestStore} from '@/stores/test.js'

var phonePatten = /^1[35789]\d{9}$/
var codePatten = /^\d{4}$/

const store = useTestStore()
const router = useRouter()
const phone = ref("");
const code = ref("");

const isPhoneTrue = ref(true)
const isCodeTrue = ref(true)

const open = (msg) => {
    ElMessage(msg)
}
const openVn = () => {
    ElMessage({
        message: h('p', null, [
            h('span', null, '消息'),
            h('i', { style: 'color: teal' }, '文字'),
        ]),
    })
}

function isPhone() {
    isPhoneTrue.value = phonePatten.test(phone.value)
}
function isCode() {
    if (code.value == '') {
        isCodeTrue.value = false
    } else {
        isCodeTrue.value = codePatten.test(code.value)
    }
}
function getCode() {
    axios.get('http://192.168.110.221:8080/user/sendMessageController',{
        params:{
            phone:phone.value
        }
    })
    .then((resp)=>{
        if(resp.data.code==200){
            open("验证码已发送")
            sessionStorage.setItem('code',resp.data.datas)
            
        }else{
            open(resp.data.msg)
        }     
    })
}
function submit() {
    if (phone.value == "" || code.value == "") {
        alert("请输入账号和验证码")
        isPhoneTrue.value = false
        isCodeTrue.value = false
    } else {
        if(code.value==sessionStorage.getItem("code")){
            router.push("/change-forget-password")
            store.changePasswordPhone = phone.value
        }else{
            open("验证码错误")
        }
    }
}
</script>

<template>
    <div class="back-box">
        <div class="box">
            <h1 class="header">忘记密码</h1>
            <div class="body">
                <li>
                    <div>
                        <span class="iconfont icon-shouji" style="font-size: 18px;"></span>
                        <input type="text" v-model="phone" placeholder="请输入手机号" @keyup="isPhone()" @keyup.enter="submit()">
                    </div>
                    <p v-if="!isPhoneTrue">*请输入正确的手机号</p>
                </li>
                <li>
                    <div class="code-div">
                        <span class="iconfont">&#xe624;</span>
                        <input type="text" v-model="code" placeholder="请输入验证码" class="code-text" @keyup="isCode()"
                            @keyup.enter="submit()">
                    </div>
                    <span class="code-pic">
                        <TimeBtn @click-event="getCode()"></TimeBtn>
                    </span>
                    <p v-if="!isCodeTrue">*请输入正确的验证码</p>
                </li>
                <li>
                    <button class="login-btn" @click="submit()">确认</button>
                </li>
            </div>
        </div>
    </div>
</template>

<style scoped>
* {
    text-align: center;
    list-style: none;
    text-decoration: none;
}

.back-box {
    position: absolute;
    width: 100%;
    height: 100%;
    background: url("../../assets/img/b5.png");
    background-size: 100% 100%;
}

.box {
    margin-top: 200px;
}

.header {
    margin-top: 20px;
    font-size: 16px;
}

.body {
    margin-top: 20px;
    overflow: hidden;
}

.body::after {
    display: block;
    content: "";
    clear: both;
}

.iconfont {
    font-family: "iconfont" !important;
    font-size: 16px;
    font-style: normal;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    margin-right: 10px;
}

input {
    text-align: left;
    width: 200px;
    height: 30px;
    outline: none;
    font-size: 16px;
    border: transparent;
    background-color: transparent;
}

.code-text {
    width: 120px;
}

.code-pic {
    display: inline-block;
    height: 32px;
    line-height: 32px;
    margin-left: 8px;
    width: 70px;
    /* border: 1px solid #d6d6d6; */
    vertical-align: top;
    background-color: #fff;
    font-size: 12px;
}

.body>li {
    margin-top: 20px;
}

.body>li::after {
    margin: 0 auto;
    display: block;
    content: "";
    clear: both;
}

.body>li>div~p {
    width: 260px;
    margin: 0 auto;
    color: red;
    text-align: left;
    text-indent: 10px;
}

.body>li>div {
    overflow: hidden;
    display: inline-block;
    border: 1px solid #d6d6d6;
    width: 260px;
    border-radius: 20px;
    background-color: #fff;
}

.code-div {
    width: 180px !important;
    right: 40px;
}

.login-btn {
    width: 260px;
    height: 30px;
    border-radius: 20px;
    border: transparent;
    font-size: 16px;
    font-weight: 600;
    color: #fff;
    background-color: rgba(11, 86, 247, 0.781);
}

li>p {
    font-size: 13px;
}</style>